package com.xiaowan.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.math.BigInteger;
import java.util.List;

/**
 * Spring Mvc配置
 * 重点:@EnableAspectJAutoProxy:如果你想aop也能拦截到controller层，那么必须打上这个注解，值必须为true,暂时xml设为false会报错
 *                              设置为true，意思是将类通过cglib代理，
 *                              aop:原理就是要通过代理之后的类才会生效，controller只是普通类只能通过cglib代理
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.xiaowan.*.controller",
        excludeFilters = @ComponentScan.Filter(Service.class)) // 扫描controller类,排除@Service注解的类
@EnableAspectJAutoProxy(proxyTargetClass = true) // 设置为true将类改为cglib代理模式
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        HttpMessageConverter<?> converter = fastConverter;

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //mybatis plu 通用枚举配置 设置WriteEnumUsingToString
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteEnumUsingToString);

        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
        serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
        fastJsonConfig.setSerializeConfig(serializeConfig);

        fastConverter.setFastJsonConfig(fastJsonConfig);

        converters.add(fastConverter);
    }

}
